package afi.ch1;

/**
 * @author Pavel Belevich
 *         Date: 6/21/12
 *         Time: 8:49 PM
 */
public class SquareRoot {

    static short mySqrtSearch(int input) {
        short start = 0;
        short end = Short.MAX_VALUE;
        short m;
        int n;
        while (start <= end) {
            m = (short) (start + (end - start) / 2);
            n = m*m;
            if (n > input) {
                end = (short) (m - 1);
            } else if (n < input) {
                start = (short) (m + 1);
            } else {
                return m;
            }
        }
        return start;
    }
    
    public static void main(String[] args) {
        System.out.println(mySqrtSearch(1000));
    }
    
}
